package com.fjeld.middleware.activemq;

import com.fjeld.middleware.jaxb.ObjectFactory;
import com.fjeld.middleware.jaxb.V1Message;
import com.fjeld.middleware.util.collection.ObservableArrayList;
import com.fjeld.middleware.util.collection.ObservableHashMap;
import org.apache.log4j.Logger;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User: fjeld
 * Date: 29/12/2011
 * Time: 11:21
 *
 * Listens to messages and puts them straight into the observable list
 */
public class MessagesListener extends TopicListener
{
    private static final Logger LOGGER = Logger.getLogger(MessagesListener.class);
    private ObservableArrayList messageList = new ObservableArrayList(500);

    public MessagesListener(String topicString, String publisherName) throws JMSException
    {
        super(topicString, publisherName);
        LOGGER.info(publisherName);

    }

    public ObservableArrayList getMessageList()
    {
        return messageList;
    }

    public void startConnection()
    {
        super.startConnection();
    }

    public void onMessage(Message message)
    {
        ObjectMessage objMsg = (ObjectMessage) message;

        try
        {
            Serializable msg = objMsg.getObject();

            if (msg instanceof V1Message)
            {
                messageList.add(msg);
            } else
            {
                LOGGER.error("Received unsolicited message: " + msg.toString());
            }
        } catch (JMSException e)
        {
            LOGGER.error(logPrefix + "Could not unmarshal message:", e);
        }
    }
}

